package com.zenithmind.mall.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.mall.pojo.dto.UserAddressDTO;
import com.zenithmind.mall.pojo.vo.UserAddressVO;
import com.zenithmind.mall.service.UserAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址控制器
 * 
 * @author ZenithMind Team
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/mall/address")
@RequiredArgsConstructor
@RequireAuth
@Tag(name = "用户地址管理", description = "用户地址相关接口")
public class UserAddressController {

    private final UserAddressService addressService;

    @GetMapping
    @Operation(summary = "获取用户地址列表", description = "获取当前用户的所有地址")
    public Result<List<UserAddressVO>> getUserAddressList() {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 获取地址列表", userId);
        List<UserAddressVO> result = addressService.getUserAddressList(userId);
        return Result.success(result);
    }

    @GetMapping("/default")
    @Operation(summary = "获取默认地址", description = "获取当前用户的默认地址")
    public Result<UserAddressVO> getDefaultAddress() {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 获取默认地址", userId);
        UserAddressVO result = addressService.getDefaultAddress(userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取地址详情", description = "根据地址ID获取详细信息")
    @Parameter(name = "id", description = "地址ID", required = true)
    public Result<UserAddressVO> getAddressById(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 获取地址详情: {}", userId, id);
        UserAddressVO result = addressService.getAddressById(id, userId);
        return Result.success(result);
    }

    @PostMapping
    @Operation(summary = "创建地址", description = "创建新的收货地址")
    @Log(description = "创建收货地址", operationType = OperationTypeEnum.ADD)
    public Result<String> createAddress(@Validated @RequestBody UserAddressDTO addressDTO) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 创建地址: {}", userId, addressDTO.getReceiverName());
        String addressId = addressService.createAddress(addressDTO, userId);
        return Result.success(addressId);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新地址", description = "更新收货地址信息")
    @Parameter(name = "id", description = "地址ID", required = true)
    @Log(description = "更新收货地址", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> updateAddress(@PathVariable String id, 
                                       @Validated @RequestBody UserAddressDTO addressDTO) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 更新地址: {}", userId, id);
        Boolean result = addressService.updateAddress(id, addressDTO, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除地址", description = "删除收货地址")
    @Parameter(name = "id", description = "地址ID", required = true)
    @Log(description = "删除收货地址", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteAddress(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 删除地址: {}", userId, id);
        Boolean result = addressService.deleteAddress(id, userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/default")
    @Operation(summary = "设置默认地址", description = "将指定地址设置为默认地址")
    @Parameter(name = "id", description = "地址ID", required = true)
    @Log(description = "设置默认地址", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> setDefaultAddress(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 设置默认地址: {}", userId, id);
        Boolean result = addressService.setDefaultAddress(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除地址", description = "批量删除收货地址")
    @Log(description = "批量删除收货地址", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteAddressesBatch(@RequestBody List<String> ids) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 批量删除地址，数量: {}", userId, ids.size());
        Boolean result = addressService.deleteAddressesBatch(ids, userId);
        return Result.success(result);
    }
}
